import { Component, Injector, OnInit, Input, ViewChild, EventEmitter, Output } from '@angular/core';
import { RoleEditDto, RoleServiceProxy, CreateOrUpdateRoleInput } from '@shared/service-proxies/service-proxies'
import { ModalComponentBase } from '@shared/component-base';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NzFormatEmitEvent } from 'ng-zorro-antd';
import { finalize } from 'rxjs/operators';

@Component({
    moduleId: module.id,
    selector: 'create-or-edit-role',
    templateUrl: 'create-or-edit-role.component.html',
    styleUrls: ['create-or-edit-role.component.scss']
})
export class CreateOrEditRoleComponent extends ModalComponentBase implements OnInit {
    @Input() id: number;
    @Input() name: string;
    @ViewChild('treeCom') treeCom;
    PermissionNameList: any;

    ay = new Array;
    nodes: any;
    defaultCheckedKeys: any;//选中的节点
    defaultExpandedKeys: any;
    defaultSelectedKeys: any;//张开的节点
    nzTitle: string;
    searchValue: any;
    role: RoleEditDto = new RoleEditDto();
    form: FormGroup;
    constructor(
        injector: Injector,
        private _roleService: RoleServiceProxy,
        private fb: FormBuilder
    ) {
        super(injector);
    }

    // defaultCheckedKeys = ['0-0-0-0', '0-0-0-1', '0-0-0-2'];//选中的节点
    // defaultSelectedKeys = [];
    // defaultExpandedKeys = ['0-0', '0-0-0'];//张开的节点
    ngOnInit(): void {
        this.form = this.fb.group({
            roleName: [null, [Validators.required]], // 名称
            displayName: [null, [Validators.required]],
            description: [null]
        });
        if (this.id) {

            this.nzTitle = '编辑角色';
            this.show(this.id);
        } else {
            this.nzTitle = '添加角色';
            //this.defaultExpandedKeys = ['Pages', 'Pages.DingTalk', 'Pages.Users', 'Pages.Roles', 'Pages.WeChat', 'Pages.SystemManagement'];
            this.show();

        }

    }
    show(roleId?: number) {
        this._roleService.getRoleForEdit(roleId).subscribe(result => {
            this.role = result.role;
            this.defaultCheckedKeys = result.grantedPermissionNames;
            this.defaultExpandedKeys = result.grantedPermissionNames;
            this.nodes = result.getRoleNodes;
            //console.log(this.nodes)
        });

    }
    nzEvent(event: NzFormatEmitEvent): void {
        this.ay = new Array;
        var nodeslist = event.nodes;
    }

    save() {

        for (const i in this.form.controls) {
            this.form.controls[i].markAsDirty();
            this.form.controls[i].updateValueAndValidity();
        }

        if (!this.form.valid) {
            return;
        }

        // var anyCkv = this.treeCom.getCheckedNodeList().map(a => a.key);
        let result = this.treeCom.getCheckedNodeList();
        let childrenstr = this.handel(result);
        const input = new CreateOrUpdateRoleInput();
        input.role = this.role;
        input.grantedPermissionNames = childrenstr;
        this.saving = true;
        this._roleService.createOrUpdateRole(input)
            .pipe(finalize(() => this.saving = false))
            .subscribe(() => {
                this.notify.info(this.l('保存成功'));
                this.success();
            });

    }

    ckeckstr = [];
    handel(pam: any[]): any[] {
        pam.forEach(item => {
            this.ckeckstr.push(item.key);
            if (item.children) {
                this.handel(item.children);
            }
        });
        return this.ckeckstr;
    }


}
